<?php
/*******************************************************
*
* Page:    app__list_object.class
* Author:  Tom Lamm
* Purpose: Base class for creating persistent data objects exist in
*                have ordinal lists
* Created: 01/29/06
* Version: 1.0
*
* Modified:
* 
*
*******************************************************/

/******************************************************
*   A list object extends app_object and exists in an ordered list
*   $fields, $table, and $id_field belong to app_object
*   $ordinal_field is the name of the column that stores this object's
*   order in the list. The order is 1 ... n
*   $sub_list_parent is for lists that have categories, it is the column in the
*   table that stores the category id
*
*   If a list has no sub_lists, the ordinal is unique to the entire table
*   If a list has sub_lists, the ordinal field is unique to the sublist
*   The sub_list / ordinal pair is unique to the entire table
*
*   app_list_object includes methods to move itself up or down one level
*   in the ordinal order, swapping itself with the next object in that order
*/
class app_list_object extends app_object {

   var $ordinal_field = NULL;
   var $sub_list_parent = NULL;

   function app_list_object($fields = array(), $table, $id_field, $ordinal_field, $sub_list_parent = NULL){

      // set up the parent object
      app_object::app_object ($fields, $table, $id_field);

      // add the ordinal and sub_list_parent so that object can save and restore them

      // set the ordinal and sub_list_parent local
      $this->ordinal_field = $ordinal_field;
      $this->sub_list_parent = $sub_list_parent;
    } // end function app_list_object

   function save () {
      // if going to do an insert, set the ordinal value to the next highest one available
      if (!$this->id_value) {
         $this->values[$this->ordinal_field] = $this->get_hi_ordinal();
      } // end if

      app_object::save ();
   } // end function save

    function get_hi_ordinal(){
        //this is used by the insert query to assure that a new record always has the next ordinal value
        $query = 'SELECT max(' . $this->ordinal_field . ') FROM ' . $this->table;

         // if in a sub_list, add the sub_list_parent
         if (isset ($this->sub_list_parent)) {
            $query .= " WHERE " . $this->sub_list_parent . " = '" . $this->values[$this->sub_list_parent] . "'";
         } // end if

        $result = app_data::mysql_query($query);
        $hi_ordinal = mysql_result($result, 0);

        if ($hi_ordinal == null) {
           return 0;
        } else {
           return $hi_ordinal + 1;
        } // end if null
    } // end function get_hi_ordinal

   function move_ordinal ($direction, $additional_where = ''){
      if ($direction == 'down') {
         $oper = '>';
         $order = 'ASC';
      } else {
         $oper = '<';
         $order = 'DESC';
      } //end if
      
      $query = 'SELECT ' . $this->id_field . ', ' . $this->ordinal_field 
             . ' FROM ' . $this->table 
             . ' WHERE ' . $this->ordinal_field . ' ' . $oper . ' ' . $this->values[$this->ordinal_field];

      // in a sub_list?
      if (isset ($this->sub_list_parent)) {
         $query .= ' AND ' . $this->sub_list_parent . ' = ' . $this->values[$this->sub_list_parent] . ' ';
      } // end if
   
      // did client pass addtional where clause info?
      if (strlen ($additional_where) > 0) {
         $query .= $additional_where;
      } // end if
   
      $query.= 'ORDER BY ' . $this->ordinal_field . ' ' . $order . ' LIMIT 1';
      $result = mysql_query($query);
      $row = mysql_fetch_row($result);

      if (mysql_num_rows($result) > 0) {
        $other_id = $row[0];
        $other_ordinal = $row[1];
      
        $query = 'UPDATE ' . $this->table . ' SET ' . $this->ordinal_field . ' = ' . $other_ordinal 
               . ' WHERE ' . $this->id_field . ' = ' . $this->id_value;
        mysql_query($query);
   
        $query = 'UPDATE ' . $this->table . ' SET ' . $this->ordinal_field 
               . ' = ' . $this->values[$this->ordinal_field] 
               . ' WHERE ' . $this->id_field . ' = ' . $other_id;
        mysql_query($query);
        $this->values[$this->ordinal_field] = $other_ordinal;
     } // end if found a category to swap with
   } // end of function move_ordinal
}//end class app_list_object
